package com.longrise.android.byjk.plugins.tabfirst.answerQuestion;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.NetUtil;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by sujizhong on 16/10/17.
 * 提问
 */
public class CommitQuestionActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "CommitQuestionActivity";

    public static final String RESULT_BOOLEAN_STATUS = "commitquesionactivity_result";

    private Toolbar mTitle;

    private EditText mEdit1;
    private EditText mEdit2;

    private TextView mTextCommit;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_commitquestion;
    }


    @Override
    public void initView() {
        mEdit1 = (EditText) findViewById(R.id.commitquestionactivity_edit);
        mEdit2 = (EditText) findViewById(R.id.commitquestionactivity_edit1);

        mTextCommit = (TextView) findViewById(R.id.commitquestionactivity_textcommit);
        initTitle();
        regEvent();

        Tracker_Google.getInstance().addGoogleScreenName("提问");
    }

    private void initTitle() {
        mTitle = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTitle.setTitle("提问");
        setSupportActionBar(mTitle);
        mTitle.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close(false);
            }
        });
    }

    private void regEvent() {
        mEdit1.addTextChangedListener(mTextWatcher);
        mTextCommit.setOnClickListener(this);
    }

    private void commitAnswer() {
        String edit1 = mEdit1.getText().toString().trim();
        String edit2 = mEdit2.getText().toString().trim();
        if (TextUtils.isEmpty(edit1)) {
            DZZWTools.showToast(this, getString(R.string.wtnr), Toast.LENGTH_SHORT);
            return;

        }
        if (!edit1.endsWith("？") && !edit1.endsWith("?")) {
            DZZWTools.showToast(this, getString(R.string.wtms_wh), Toast.LENGTH_SHORT);
            return;
        }
        EntityBean userBean = new EntityBean();
        userBean.set("cardno", UserInfor.getInstance().getUsersfzh());
        userBean.set("title", edit1);
        userBean.set("newstext", TextUtils.isEmpty(edit2) ? "" : edit2);
        EntityBean bean = new EntityBean();
        bean.set("bean", userBean);
        commitRequest(bean);
    }

    private void commitRequest(EntityBean bean) {
        DZZWTools.showWaitDialog(this);
        toogleButState(false);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_qa_iNewQuestion", bean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int state = bean.getInt("resultstate");
                    if (state == 1) {
                        parseData(bean);
                    } else {
                        //未知情况, toast server 返回 desc
                        DZZWTools.showToast(CommitQuestionActivity.this, bean.getString("resultdesc") + "", Toast.LENGTH_SHORT);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
                toogleButState(true);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(CommitQuestionActivity.this, CommitQuestionActivity.this.getString(R.string.networkerror), Toast.LENGTH_SHORT);
            }
        });
    }

    private void toogleButState(boolean isBoolean) {
        mTextCommit.setEnabled(isBoolean);
        mTextCommit.setClickable(isBoolean);
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            try {
                String msg1 = mEdit1.getText().toString().trim();
                if (msg1.length() <= 0) {
                    switchClickAble(false, R.drawable.shape_hui);
                } else {
                    switchClickAble(true, R.drawable.selecter_yellowbtn);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void switchClickAble(boolean bool, int drawable) {
        mTextCommit.setEnabled(bool);
        mTextCommit.setClickable(bool);
        mTextCommit.setBackgroundResource(drawable);
    }

    /**
     * 如果提交提问成功，设置必要刷新问答最新界面
     */
    private void parseData(EntityBean bean) {
        try {
            boolean reult = bean.getBoolean("result");
            if (reult) {
                close(reult);
            } else {
                //未知情况,toast server返回desc
                DZZWTools.showToast(this, bean.getString("resultdesc") + "", Toast.LENGTH_SHORT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void close(boolean bool) {
        setResult(100, new Intent().putExtra(RESULT_BOOLEAN_STATUS, bool));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commitquestionactivity_textcommit:
                if (checkNetwork()) {
                    commitAnswer();
                    Tracker_Google.getInstance().addEvent("QA", "Ask-question", "提问");
                }
                break;
        }
    }

    private boolean checkNetwork() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
